//
//  AuthController.swift
//  App
//
//  Created by chenyh on 2018/5/28.
//

import Vapor
import FluentSQLite
/// Controls basic CRUD operations on `Account`s.
//到时候需要将这些提示的字段 都提取出来 到一个model中

final class AuthController {
    
    func register(_ req: Request) throws ->  Future<Response> {
        print(req)
        let decodeResult = try req
            .content
            .decode(RequestFields.self)
            .flatMap { (fields) -> Future<Response> in

            return try Account.userExist(req: req, rusername: fields.username).flatMap{ (account) -> EventLoopFuture<Response> in
                
                
                    if let _ = account  {
                        return try callFail("用户已存在",status: 0,req: req)
                    }
                    //创建用户
                    let account = Account(username: fields.username, password: fields.password)
                    let queryResult = account.create(on: req)
                        .flatMap{ (account) -> EventLoopFuture<Response> in
                        account.password = nil
                        let result1 = AuthResult(message: "注册成功", status: 1, result: account)
                        return result1.encode(status: .ok, for: req)
                    }
                    return queryResult
                }
            }

        return decodeResult
    }
    
    func login(_ req: Request) throws ->  Future<Response> {

        return try req
            .content
            .decode(RequestFields.self)
            .flatMap { (fields) -> Future<Response> in
                
                    return try Account
                       .login(req: req,
                             rusername: fields.username,
                             rpassword: fields.password)
                        .flatMap { (account) -> EventLoopFuture<Response> in
                        
                        guard let result = account else {
//                            return try callFail("登录失败,用户名或密码错误!", status: 0, req: req)
                            throw Abort.init(HTTPStatus.custom(code: 0, reasonPhrase: "登录失败,用户名或密码错误!"), headers: req.http.headers, reason: "登录失败,用户名或密码错误!")
                        }
                        
                        result.last_login = "\(Int(Date().timeIntervalSince1970))"
                        
                        
                        return result.update(on: req).flatMap{ (account) -> EventLoopFuture<Response> in
                            account.password = nil
                            let result1 = AuthResult(message: "登录成功", status: 1, result: account)
                            return  result1.encode(status: .ok, for: req)
                        }
                }
            }

    }
}
struct RequestFields: Content {
    var username: String
    var password: String
}

/// Allows `Todo` to be used as a dynamic parameter in route definitions.
extension RequestFields: Parameter {
    static func resolveParameter(_ parameter: String, on container: Container) throws -> String {
        
        print(parameter)
        return parameter
    }
    
    typealias ResolvedParameter = String
}

struct AuthResult: Content, ResponseResultFormatProtocol {
    typealias ResultType = Account
    var message: String
    var status: Int
    var result: Account?
}
